package com.shanghai.market.plshnaghai.model.login;

import com.google.gson.Gson;
import com.shanghai.market.plshnaghai.abstracts.AbstractModelGet;
import com.shanghai.market.plshnaghai.constants.UrlConstants;
import com.shanghai.market.plshnaghai.entity.login.Code;
import com.shanghai.market.plshnaghai.entity.login.CodeResponse;
import com.shanghai.market.plshnaghai.interfaces.IObjectView;
import com.shanghai.market.plshnaghai.net.HttpManager;

/**
 * Created by Administrator on 2017/8/15.
 */

public class CodeModel extends AbstractModelGet {

    private IObjectView<String> view;

    public CodeModel(IObjectView<String> view) {
        super();
        this.view = view;
    }

    @Override
    public void getData(String... args) {
        HttpManager manager = HttpManager.getInstance();

        manager.get(UrlConstants.GET_VERIFYCODE + args[0], lisenter(), errorListener());

    }

    @Override
    protected void error(String message) {
        view.failure(message);
    }

    @Override
    protected void success(String result) {
        final Gson gson = new Gson();
        CodeResponse t = gson.fromJson(result, CodeResponse.class);

        String message = t.getErrorMessage();
        final String status = t.getStatus();
        if ("OK".equals(status)) {
            Code code = t.getData();
            view.success(code.getCode());
        } else {
            view.failure(message);
        }
    }
}
